package com.bdsys.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bdsys.common.action.BasicAction;
import com.bdsys.common.constants.Constants;
import com.bdsys.form.User;
import com.bdsys.service.UserService;
import com.bdsys.servicedto.UserDetailDTO;
import com.bdsys.servicedto.UserServiceDTO;
import com.bdsys.vo.UserVo;

@SuppressWarnings("serial")
public class UserAction extends BasicAction implements Constants{
	
	User user;
	
	private UserService  userService = (UserService)getService(USER_SERVICE);
	
	public String addUser(){
		UserService  userService = (UserService)getService(USER_SERVICE);
		UserServiceDTO inServiceDTO = new UserServiceDTO();
		inServiceDTO.setUserDetailDTO(this.fromFormToUserDetailDto());
		userService.addUser(inServiceDTO);
		return EDITSUCCESS;
	}
	
	public String deleteUserType(){
		UserService  userService = (UserService)getService(USER_SERVICE);
		userService.deleteUserType(user.getType());
		return "editUserType";
	}
	
	public String checkLoginUser(){
		UserService  userService = (UserService)getService(USER_SERVICE);
		String type = userService.checkLoginUser(user.getName(), user.getPassword());
		if(type == null){
			getRequest().put("nouser", "nouser");
			return "loginFail";
		}else{
			return "loginSuccess";
		}
		
	}
	
	public String addUserType(){
		UserService  userService = (UserService)getService(USER_SERVICE);
		userService.addUserType(Arrays.asList(user.getUserTypes()));
		return "editUserType";
	}
	
	public String selectAllUserTypes(){
		UserService  userService = (UserService)getService(USER_SERVICE);
		Map<String, String> userTypes = userService.selectAllUserTypes();
		getRequest().put("userTypes", userTypes);
		if(user != null && "userTypeList".equals(user.getAction())){
			setPage("/user/userTypeList.jsp");
		}else {
			setPage("/user/editUser.jsp");
		}
		
		return "selectAllUseTypes";
	}
	
	private UserDetailDTO fromFormToUserDetailDto(){
		user = (user == null ? new User() : user);
		UserDetailDTO detailDTO = new UserDetailDTO();
		detailDTO.setEmail(user.getEmail());
		detailDTO.setId(user.getId());
		detailDTO.setName(user.getName());
		detailDTO.setPassword(user.getPassword());
		detailDTO.setPhone(user.getPhone());
		detailDTO.setType(user.getType());
		detailDTO.setuClass(user.getUclass());
		return detailDTO;
	}

	public String selectByConditions(){
		UserServiceDTO inServiceDTO = new UserServiceDTO();
		inServiceDTO.setUserDetailDTO(this.fromFormToUserDetailDto());
		UserServiceDTO outServiceDTO = userService.selectUsersByConditions(inServiceDTO);
		List<UserDetailDTO> list = outServiceDTO.getList();
		
		List<UserVo> usersVo = new ArrayList<UserVo>();
		
		for(UserDetailDTO detailDTO:list){
			usersVo.add(this.fromDetailDTOToVo(detailDTO));
		}
		
		if(user != null){
			if("forUpdate".equals(user.getAction())){
				getRequest().put("userVo", usersVo.get(0));
			}else{
				getRequest().put("userList", usersVo);
			}
		}else{
			getRequest().put("userList", usersVo);
		}
		
		
		return SELECTBYCONDITIONS;
		
	}
	
	public User getUser() {
		return user;
	}
	
	private UserVo fromDetailDTOToVo(UserDetailDTO detailDTO){
		UserVo vo = new UserVo();
		vo.setId(detailDTO.getId());
		vo.setName(detailDTO.getName());
		vo.setType(detailDTO.getType());
		vo.setPhone(detailDTO.getPhone());
		vo.setEmail(detailDTO.getEmail());
		vo.setPassword(detailDTO.getPassword());
		vo.setUclass(detailDTO.getuClass());
		vo.setTypeName(detailDTO.getTypeName());
		return vo;
	}
	
	public String deleteUser(){
		UserService  userService = (UserService)getService(USER_SERVICE);
		userService.deleteUser(user.getId());
		return EDITSUCCESS;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	
}
